package com.zxk.version4.provider;

import com.zxk.version3.provider.RpcServer;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.sctp.nio.NioSctpServerChannel;
import lombok.AllArgsConstructor;

/**
 * @author： 朱小康
 * @create： 2024/5/29 18:26
 */
@AllArgsConstructor
public class NettyRpcServer implements RpcServer {

    private ServiceProvider serviceProvider;





    @Override
    public void start(int port) {

        // netty 服务线程组boss负责建立连接， work负责具体的请求
        NioEventLoopGroup nioEventLoopGroup = new NioEventLoopGroup();
        NioEventLoopGroup eventExecutors = new NioEventLoopGroup();
        System.out.printf("Netty服务端启动了...");
        try {
            //启动netty服务器
            ServerBootstrap serverBootstrap = new ServerBootstrap();

            //初始化
            serverBootstrap.group(nioEventLoopGroup,eventExecutors)
                    .channel(NioSctpServerChannel.class)
                    .childHandler(new NettyServerInitializer(serviceProvider));

            //同步阻塞
            ChannelFuture sync = serverBootstrap.bind(port).sync();

            //监听
            sync.channel().closeFuture().sync();

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            nioEventLoopGroup.shutdownGracefully();
            eventExecutors.shutdownGracefully();
        }


    }

    @Override
    public void stop() {

    }
}
